<?php
namespace Kuoqi\DbDriver\Sql;

/**
* remark:数据库Sql对象
*/
class Sql{
	public $SIMPLE_COL = 'col';
	public $scale = '';
	public $driver = null;
	public $sql = '';//sql语句
	
	public function __construct($driver)
	{
		$this->driver = $driver;
	}
	
	//执行sql的部分
	//scale
	public function scale($scale){
		$this->scale = strtoupper($scale);
		
		
		switch($this->scale){
			case 'FIND':return $this->scaleFind();	
			case 'SELECT':return $this->scaleSelect();
			case 'TABLE-NAMES':return $this->scaleTablenames();
			case 'INSERT':return $this->scaleInsert();
			case 'UPDATE':return $this->scaleUpdate();
			case 'DELETE':return $this->scaleDelete();
			case 'COUNT':return $this->scaleCount();
			case 'COLUMNS':return $this->scaleColumns();
			default:throw new \Kuoqi\Error('暂不支持【'.$this->scale.'】关键字');
		}
	}
	
	public function scaleFind(){
		$this->sql = sprintf('SELECT %s FROM %s %s %s %s %s LIMIT 1',
			$this->driver->objfield?$this->driver->objfield->output():'*',//字段
			$this->driver->objtable->output(),//table相关处理操作
			$this->driver->objjoin?$this->driver->objjoin->output():'',//join
			$this->driver->objwhere?$this->driver->objwhere->output():'',//where条件处理
			$this->driver->objgroup?$this->driver->objgroup:'',//groupBy
			$this->driver->objorder?$this->driver->objorder:''//orderby
		);
		return str_replace('***',$this->driver->prefix,$this->sql);
	}
	
	//scaleSelect
	public function scaleSelect(){
		$this->sql = sprintf('SELECT %s FROM %s %s %s %s %s %s',
			$this->driver->objfield?$this->driver->objfield->output():'*',//字段
			$this->driver->objtable->output(),//table相关处理操作
			$this->driver->objjoin?$this->driver->objjoin->output():'',//join
			$this->driver->objwhere?$this->driver->objwhere->output():'',//where条件处理
			$this->driver->objgroup?$this->driver->objgroup:'',//groupBy
			$this->driver->objorder?$this->driver->objorder:'',//orderby
			$this->driver->objlimit?$this->driver->objlimit:''//objlimit
		);
		return str_replace('***',$this->driver->prefix,$this->sql);
	}
	
	//scaleTablenames
	public function scaleTablenames(){
		$this->sql = sprintf('SELECT COLUMN_NAME as %s FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = \'%s\' and TABLE_NAME = \'%s\'',
			$this->SIMPLE_COL,
			$this->driver->getDbname(),
			$this->driver->objtable->output()
		);
		return str_replace('***',$this->driver->prefix,$this->sql);
	}
	
	//scaleInsert
	public function scaleInsert(){
		if(!$this->driver->objdata){
			throw new \Kuoqi\Error('[insert error]写入数据不能为空');
		}
		$this->sql = sprintf('INSERT INTO %s %s',
			$this->driver->objtable->output(),//table相关处理操作
			$this->driver->objdata->output()//where条件处理
		);
		return str_replace('***',$this->driver->prefix,$this->sql);
	}
	
	//scaleUpdate
	public function scaleUpdate(){
		if(!$this->driver->objdata){
			throw new \Kuoqi\Error('[update error]写入数据不能为空');
		}
		if(!$this->driver->objwhere){
			throw new \Kuoqi\Error('[update error]where条件为必填');
		}
		$this->sql = sprintf('UPDATE %s SET %s %s',
			$this->driver->objtable->output(),//table相关处理操作
			$this->driver->objdata->outputUpdate(),
			$this->driver->objwhere->output()
		);
		return str_replace('***',$this->driver->prefix,$this->sql);
	}
	
	//scaleDelete
	public function scaleDelete(){
		if(!$this->driver->objwhere){
			throw new \Kuoqi\Error('[delete error]where条件为必填');
		}
		$this->sql = sprintf('DELETE FROM %s %s %s %s',
			$this->driver->objtable->output(),//table相关处理操作
			$this->driver->objwhere->output(),
			$this->driver->objorder?$this->driver->objorder:'',//orderby
			$this->driver->objlimit?$this->driver->objlimit:''//objlimit
			
		);
		return str_replace('***',$this->driver->prefix,$this->sql);	
	}
	
	//scaleCount
	public function scaleCount(){
		$this->sql = sprintf('SELECT %s FROM %s %s %s %s %s %s',
			'COUNT( * ) AS COUNT',//字段
			$this->driver->objtable->output(),//table相关处理操作
            $this->driver->objjoin?$this->driver->objjoin->output():'',//join
			$this->driver->objwhere?$this->driver->objwhere->output():'',//where条件处理
			$this->driver->objgroup?$this->driver->objgroup:'',//groupBy
			$this->driver->objorder?$this->driver->objorder:'',//orderby
			$this->driver->objlimit?$this->driver->objlimit:''//objlimit
		);
		return str_replace('***',$this->driver->prefix,$this->sql);		
	}
	
	//获取数据表字段的详细信息
	public function scaleColumns(){
		$this->sql = sprintf('SELECT %s,%s,%s,%s FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = \'%s\' and TABLE_NAME = \'%s\'',
			'COLUMN_NAME as colname',//字段名：COLUMN_NAME：keyname,
			'EXTRA as extra',//额外的数据信息，字段自动增加会写到这里面【auto_increment】
			'DATA_TYPE as data_type',//数据类型【int,varchar....】
			'COLUMN_COMMENT as column_comment',//字段备注信息
            //COLUMN_NAME as colname,EXTRA as extra ,DATA_TYPE as data_type,COLUMN_COMMENDS as column_commen
			$this->driver->getDbname(),
			$this->driver->objtable->output()
		);
		return str_replace('***',$this->driver->prefix,$this->sql);
	}
	
}
